﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GuildofHeroes
{
    class AnimatedSprite
    {
        private Texture2D spriteTexture;
        //private List<Rectangle> rectangles;
        private Vector2 spriteOrigin;
        private Vector2 spritePosition;
        private int frameCount;
        private float timePerFrame;
        private float totalElapsed;
        private float scale;
        private int currentFrame;
        private int framesPerSec;
        private int framesPerRow;
        private int frameWidth;
        private int frameHeight;

        public void LoadSprite(Texture2D _texture2D, int _posX, int _posY, int _numFrames, int _frameWidth, int _frameHeight,
            int _horizontalSpace, int _verticalSpace, int _framesPerSec, float _scale)
        {
            spriteTexture = _texture2D;
            frameWidth = _frameWidth;
            frameHeight = _frameHeight;
            framesPerRow = spriteTexture.Width / (frameWidth + _horizontalSpace);

            frameCount = _numFrames;
            framesPerSec = _framesPerSec;
            timePerFrame = (float)1 / framesPerSec;
            currentFrame = 0;
            totalElapsed = 0;
            spriteOrigin = new Vector2(_frameWidth / 2, _frameHeight / 2);
            spritePosition = new Vector2(_posX, _posY);
            //Position = spritePosition;
            scale = _scale;
        }

        private Rectangle GetFrameRectangle(int _frameNumber)
        {
            return new Rectangle((_frameNumber % framesPerRow) * frameWidth,
                                 (_frameNumber / framesPerRow) * frameHeight,
                                   frameWidth,
                                   frameHeight
                                );
        }

        public void Update(GameTime _gameTime)
        {
            float elapsed = (float)_gameTime.ElapsedGameTime.TotalSeconds;
            totalElapsed += elapsed;

            if (totalElapsed > timePerFrame)
            {
                currentFrame++;
                currentFrame = currentFrame % frameCount;
                totalElapsed -= timePerFrame;
            }
        }

        public void Draw(SpriteBatch _spriteBatch)
        {
            _spriteBatch.Draw(spriteTexture, spritePosition, GetFrameRectangle(currentFrame), Color.White, 0,
                spriteOrigin, scale, SpriteEffects.None, 0.6f);
        }

        public void UpdatePos(float x, float y)
        {
            spritePosition.X += x;
            spritePosition.Y += y;
        }

        public void Update2(Vector2 pPos)
        {
            spritePosition.X = pPos.X;
            spritePosition.Y = pPos.Y;
        }

        public void UpdatePos(Vector2 pPos)
        {
            spritePosition.X += pPos.X;
            spritePosition.Y += pPos.Y;
        }

        public float Y
        {
            get { return this.spritePosition.Y; }
            set { this.spritePosition.Y = value; }
        }

        public float X
        {
            get { return this.spritePosition.X; }
            set { this.spritePosition.X = value; }
        }

        public int GetHeight()
        {
            return (int)(frameHeight * scale);
        }

        public int GetWidth()
        {
            return (int)(frameWidth * scale);
        }

        public Rectangle getRectangle()
        {
            return GetFrameRectangle(currentFrame);
        }

        public Vector2 getVectorPosition()
        {
            return spritePosition;
        }
    }
}
